<template>
  <div></div>
</template>
<script>
import { Toast } from "vant";
import Api from "./../api/api";

export default {
  data() {
    return {};
  },
  methods: {
    login() {
      //   const appID = "wx8f60c1b086124737";
      const appID = "wxaa84e0338222f222";
      const url = decodeURIComponent(`https://${Api.URL}/clouldyg/index.html`);

      //  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appID +"&redirect_uri=" +url +"&response_type=code&scope=snsapi_userinfo&state=STATE&component_appid=wx9a97f4498ab5f2e1#wechat_redirect";
      window.location.href =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
        appID +
        "&redirect_uri=" +
        url +
        "&response_type=code&scope=snsapi_userinfo&state=STATE&#wechat_redirect";
    },
    getOpenId(code) {
      this.$axios.get(`${Api.HTTP.getOpenId}?code=${code}`).then(res => {
        if (res.data.success) {
          localStorage.setItem("openId", res.data.data);
        //   this.$router.push({ path: "/login" });
        window.location.href = `https://${Api.URL}/clouldyg/index.html#/login`
        }
      });
    },
    GetRequest() {
      var url = location.search; //获取url中"?"符后的字符串
      var theRequest = new Object();
      if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
        }
      }
      return theRequest;
    }
  },
  mounted() {
      if(  localStorage.getItem('openId') != null && localStorage.getItem('openId') != undefined){
            window.location.href = `https://${Api.URL}/clouldyg/index.html#/login`
      }
    if (this.GetRequest().code == undefined) {
      this.login();
    } else {
      this.getOpenId(this.GetRequest().code);
    }
  }
};
</script>
<style scoped>
</style>